﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;
using Zwms.Client.Helper;

namespace Zwms.Client.ManageWms
{
    public partial class CountBillDtlSelForm : Form
    {
        private StoGoodsStock _queryRecord;
        private StoGoodsStock _selectedRecord;
        private BilCountBillDetail _insertRecord;
        private string _countBillCode; 

        public CountBillDtlSelForm(string strCountBillCode)
        {
            InitializeComponent();
            _queryRecord = new StoGoodsStock();
            _countBillCode = strCountBillCode;
            this.ResetDataPager();
        }

        private void ResetDataPager()
        {
            dataPager.PageCurrent = 1;
            dataPager.PageSize = CommonDefine.iPageSize;
        }

        private void SetQueryRecord()
        {
            this._queryRecord.WarehouseCode = this.tbsWarehouse.Tag.ToString();
            this._queryRecord.AreaCode = this.tbsArea.Tag.ToString();
            this._queryRecord.LocationCode = this.tbsLocation.Tag.ToString();
            this._queryRecord.SenderCode = this.tbsSender.Tag.ToString();
            this._queryRecord.ReceiverCode = this.tbsReceiver.Tag.ToString();
            this._queryRecord.GoodsCode = this.tbGoodsCode.Text.Trim();
            this._queryRecord.GoodsName = this.tbGoodsName.Text.Trim();
            if (this.cbGoodStatus.SelectedIndex != -1)
            {
                string sGoodStatus = CommonHelper.GetDictKeyByValue(CommonDefine.DictGoodsStatus, cbGoodStatus.SelectedItem.ToString());
                if (sGoodStatus != null)
                {
                    this._queryRecord.GoodsStatus = sGoodStatus;
                }
            }
        }

        private void LoadStockDetail(StoGoodsStock record)
        {
            ServiceRefManageWms.ServiceManageWmsClient client = new ServiceRefManageWms.ServiceManageWmsClient();
            PagerIndex pagerIndex = new PagerIndex(1, dataPager.PageSize);
            try
            {
                this.Cursor = Cursors.WaitCursor;
                IList<StoGoodsStock> recordList = client.QueryGoodsStockList(record, ref pagerIndex);
                this.stoGoodsStockBindingSource.DataSource = null;
                this.stoGoodsStockBindingSource.DataSource = recordList;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
 
        }

        private BilCountBillDetail recordConvert(StoGoodsStock stoRecord)
        {
            if (stoRecord == null)
            {
                return null;
            }
            BilCountBillDetail cbRecord = new BilCountBillDetail();
            cbRecord.Id = this._countBillCode;
            cbRecord.StockCode = stoRecord.Id;
            cbRecord.SenderCode = stoRecord.SenderCode;
            cbRecord.SenderName = stoRecord.SenderName;
            cbRecord.ReceiverCode = stoRecord.ReceiverCode;
            cbRecord.ReceiverName = stoRecord.ReceiverName;
            cbRecord.IBCode = stoRecord.IBCode;
            cbRecord.WarehouseCode = stoRecord.WarehouseCode;
            cbRecord.WarehouseName = stoRecord.WarehouseName;
            cbRecord.AreaCode = stoRecord.AreaCode;
            cbRecord.AreaName = stoRecord.AreaName;
            cbRecord.LocationCode = stoRecord.LocationCode;
            cbRecord.LocationName = stoRecord.LocationName;
            cbRecord.GoodsCode = stoRecord.GoodsCode;
            cbRecord.GoodsName = stoRecord.GoodsName;
            cbRecord.GoodsQty = stoRecord.GoodsTotalQty;
            cbRecord.CountedGoodsQty = 0;
            return cbRecord;
        }

        #region 控件函数
        private void tbsWarehouse_SelectButtonClick(object sender,EventArgs e)
        {
            InfoWms.WarehouseWnd uc = new InfoWms.WarehouseWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择仓库");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsWarehouse.SetTextBoxContent(uc.SelectedRecord.WarehouseName);
                tbsWarehouse.Tag = uc.SelectedRecord.Id;
            }
        }

        private void tbsArea_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.AreaWnd uc = new InfoWms.AreaWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择库区");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsArea.SetTextBoxContent(uc.SelectedRecord.AreaName);
                tbsArea.Tag = uc.SelectedRecord.AreaCode;
            }
        }

        private void tbsReceiver_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.CompanyWnd uc = new InfoWms.CompanyWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择收货方");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsReceiver.SetTextBoxContent(uc.SelectedRecord.CompanyName);
                tbsReceiver.Tag = uc.SelectedRecord.Id;
            }
        }

        private void tbsSender_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.CompanyWnd uc = new InfoWms.CompanyWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择发货方");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsSender.SetTextBoxContent(uc.SelectedRecord.CompanyName);
                tbsSender.Tag = uc.SelectedRecord.Id;
            }

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            this.SetQueryRecord();
            this.LoadStockDetail(this._queryRecord);
        }

        
        #endregion

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int rowIndex = this.dgv.CurrentCell.RowIndex;
            if (rowIndex < 0)
            {
                MessageBox.Show("请在列表选择要添加的记录");
                return;
            }

            if (MessageBox.Show("确定要添加该条信息？", "确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                DataGridViewRow row = dgv.Rows[rowIndex];
                this._selectedRecord = row.DataBoundItem as StoGoodsStock;
                this._insertRecord = this.recordConvert(this._selectedRecord);
                ServiceRefManageWms.ServiceManageWmsClient client = new ServiceRefManageWms.ServiceManageWmsClient();
                if (client.AddCountBillDetail(this._insertRecord))
                {
                    MessageBox.Show("添加成功！");
                    return;
                }
                else
                {
                    MessageBox.Show("添加失败，请检查所选记录！");
                    return;
                }

            }
            else return;
            
        }

        private void btnAddAll_Click(object sender, EventArgs e)
        {
            this.SetQueryRecord();
            ServiceRefManageWms.ServiceManageWmsClient client = new ServiceRefManageWms.ServiceManageWmsClient();
            IList<StoGoodsStock> recordList = client.QueryGoodsStockListOfAll(this._queryRecord);
            if (recordList == null || recordList.Count == 0)
            {
                MessageBox.Show("没有任何查询结果，请重新查询!");
                return;
            }
            int insertCount = 0;
            int errCount = 0;

            foreach (StoGoodsStock itemRecord in recordList)
            {
                this._insertRecord = this.recordConvert(itemRecord);
                if (client.AddCountBillDetail(this._insertRecord))
                    insertCount++;
                else errCount++;
            }
            string msg = string.Format("成功添加记录%d条，%d未能添加",insertCount,errCount);
            MessageBox.Show(msg);
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }


    }
}
